#include "channelCountResMessage.h"
#include "jsonUtils.h"

ChannelCountResMessage::ChannelCountResMessage(MtpProtocol* packet) : MtpMessage(packet),count(0)
{

}

ChannelCountResMessage::ChannelCountResMessage(const int& count, MtpMessage* src) : MtpMessage(new MtpProtocol(L"channelCountRes",(MtpProtocol*)src->getPacket())),count(count),res(true)
{
	
}

void ChannelCountResMessage::decodeBody()
{
	MtpMessage::decodeBody();
	if (!jsonContent.empty()) {
		JsonDoc& doc = res.jsonDeserialize(jsonContent);
		const JsonValue::ConstMemberIterator& it = doc.FindMember(L"data");
		if (it != doc.MemberEnd() && it->value.IsObject()) {
			JsonUtils::loadInt(it->value, L"count", count);
		}
	}
}

void ChannelCountResMessage::encodeBody()
{
	rapidjson::GenericDocument<rapidjson::UTF16<>>& doc = res.jsonSerialize();
	rapidjson::GenericValue<rapidjson::UTF16<>> v(rapidjson::kObjectType);
	v.AddMember(L"count", count, doc.GetAllocator());
	doc.AddMember(L"data", v, doc.GetAllocator());
	jsonContent = JsonUtils::jsonStr(doc);
	MtpMessage::encodeBody();
}

const int& ChannelCountResMessage::getCount()
{
	return count;
}
